<?php

require_once 'config.php';
require_once 'functions.php';
require_once 'umeng/Umeng.php';

$responseData = (array)simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);

$pdo = new \PDO('mysql:host=' . $mysql_config['hostname'] . ';dbname=' . $mysql_config['database'], $mysql_config['username'], $mysql_config['password']);
$pdo->exec('set names utf8');

//微信付款回调

if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {
	
	$out_trade_no = $responseData['out_trade_no']; //商户订单号
	$total_fee = $responseData['total_fee']; //订单金额/分
	$trade_no = $responseData['transaction_id']; //微信订单号
	
	$total_amount = $total_fee / 100;
	
	$type = substr($out_trade_no, 0, 1);
	
	if ($type == $type_config['type_1']) {
		//商品
	} else if ($type == $type_config['type_2']) {
		//项目

        // 写入预约记录如果购买的时候选择了预约时间
		// 修改订单状态
		// 修改用户金额
		// 修改项目销售数量
		// 修改店铺余额
		// 写入店铺资金流水
        // 写入项目购买记录如果项目类型为会员特惠区
        // 回调成功后写入消息

		//订单信息
		$order_query_sql = 'SELECT id, store_id, ctm_id, staff_id, starttime, add_time FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
		$order_query_stat = $pdo->prepare($order_query_sql);
		$order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
		if ($order_query_result) {
			$order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
			if ($order_query_row) {
				$order_id = $order_query_row['id'];
				$store_id = $order_query_row['store_id'];
				$ctm_id = $order_query_row['ctm_id'];
				$staff_id = $order_query_row['staff_id'];
				$starttime = $order_query_row['starttime'];
                $add_time = $order_query_row['add_time'];

				//订单产品信息
				$ord_pro_query_sql = 'SELECT id, pro_id, number, item_type FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
				$ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
				$ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
				$ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
				$pro_id = $ord_pro_query_row['pro_id'];
				$number = $ord_pro_query_row['number'];
                $item_type = $ord_pro_query_row['item_type'];

				//用户信息
				$custom_query_sql = 'SELECT id, custom_phone, custom_name, consume_money FROM da_custom WHERE id = :id';
				$custom_query_stat = $pdo->prepare($custom_query_sql);
				$custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
				$custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
				$custom_phone = $custom_query_row['custom_phone'];
				$custom_name = $custom_query_row['custom_name'];
				$consume_money = $custom_query_row['consume_money'];

				//店铺信息
				$store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
				$store_query_stat = $pdo->prepare($store_query_sql);
				$store_query_result = $store_query_stat->execute([':id' => $store_id]);
				$store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
				$store_money = $store_query_row['money'];


				$pdo->beginTransaction();
				
				$time = time();
				$flow_number = $custom_phone . $time . mt_rand(1000, 9999);

				//写入预约记录如果购买的时候选择了预约时间
                $last_yuyue_id = 0;
				if ($staff_id == 0) {
					$ctm_staff_insert_result = true;
				} else {
					$ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
					$ctm_staff_insert_sql = "INSERT INTO da_ctm_staff (staff_id, ctm_id, item_id, store_id, order_id, number, ctm_name, ctm_phone, status, add_time, start_time) VALUES ({$staff_id}, {$ctm_id}, {$pro_id}, {$store_id}, {$order_id}, 1, '{$ctm_name}', '{$custom_phone}', 3, {$time}, {$starttime})";
					$ctm_staff_insert_result = $pdo->exec($ctm_staff_insert_sql);
                    $last_yuyue_id = $pdo->lastInsertId();
				}
				
				//修改订单状态
				$status = $status_config['status_2'];
				if ($staff_id != 0) {
					if ($number == 1) {
						$status = 3;
					}
				}
				$order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_4']}, status = {$status}, pay_time = {$time}, update_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
				$order_status_result = $pdo->exec($order_status_sql);

				//修改用户金额
				$custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
				$custom_money_result = $pdo->exec($custom_money_sql);

				//修改项目销售数量
				$item_update_sql = "UPDATE da_item SET sell_count = sell_count + 1 WHERE id = {$pro_id}";
				$item_update_result = $pdo->exec($item_update_sql);

				//修改店铺余额
				$store_money_sql = "UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}";
				$store_money_result = $pdo->exec($store_money_sql);

				//写入店铺资金流水
				$flow_last_money = $store_money + $total_amount;
				$flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_2']}, {$moneyflow_config['pay_method_4']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
				$flow_insert_result = $pdo->exec($flow_insert_sql);

                //写入项目购买记录如果项目类型为会员特惠区
                if ($item_type == 3) {
                    $item_custom_insert_sql = "INSERT INTO da_item_custom (store_id, item_id, custom_id, number, price, add_time) VALUES ({$store_id}, {$pro_id}, {$ctm_id}, {$number}, {$total_amount}, {$time})";
                    $item_custom_insert_result = $pdo->exec($item_custom_insert_sql);
                } else {
                    $item_custom_insert_result = true;
                }
				
				if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $item_update_result && $store_money_result && $flow_insert_result && $item_custom_insert_result) {
					$pdo->commit();

                    //回调成功后写入消息

                    //产品信息
                    $item_query_sql = 'SELECT id, item_name FROM da_item WHERE id = :id';
                    $item_query_stat = $pdo->prepare($item_query_sql);
                    $item_query_result = $item_query_stat->execute([':id' => $pro_id]);
                    $item_query_row = $item_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $item_name = $item_query_row['item_name'];

                    //老板信息
                    $staff_query_sql = 'SELECT ds.id, ds.telephone FROM da_staff_store dss INNER JOIN da_staff ds ON dss.staff_id = ds.id WHERE dss.store_id = :store_id';
                    $staff_query_stat = $pdo->prepare($staff_query_sql);
                    $staff_query_result = $staff_query_stat->execute([':store_id' => $store_id]);
                    $staff_query_row = $staff_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $boss_id = $staff_query_row['id'];
                    $boss_telephone = $staff_query_row['telephone'];

                    $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $content = "【已付款】[{$custom_name}] [{$item_name}] X [{$number}]，共计 [{$total_amount}] 元";
                    $msgContent = getColoredString($content);
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('1', {$order_id}, 0, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    //收入消息
                    $content = "【新增收入】最新收入 [{$total_amount}] 元，请注意查收";
                    $msgContent = getColoredString($content);
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('10', {$order_id}, 0, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    //预约消息
                    if ($staff_id != 0) {
                        //给店铺发推送

                        //美容师信息
                        $staff_query_sql_2 = 'SELECT id, staff_name FROM da_staff WHERE id = :id';
                        $staff_query_stat_2 = $pdo->prepare($staff_query_sql_2);
                        $staff_query_result_2 = $staff_query_stat_2->execute([':id' => $staff_id]);
                        $staff_query_row_2 = $staff_query_stat_2->fetch(\PDO::FETCH_ASSOC);
                        $staff_name = $staff_query_row_2['staff_name'];

                        $yuyue_time = date('Y-m-d H:i', $starttime);
                        $content = "【已预约】[{$custom_name}] 预约了 [{$yuyue_time}] [{$staff_name}] 的 [{$item_name}]，快去确认吧";
                        $msgContent = getColoredString($content);
                        $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('2', {$order_id}, $last_yuyue_id, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                        $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                        //给美容师发推送
                        $content = "【已预约】[{$custom_name}] [{$yuyue_time}] 向您预约了 [{$item_name}] 服务";
                        $msgContent = getColoredString($content);
                        $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('2', {$order_id}, $last_yuyue_id, \"{$msgContent}\", '{$time}', {$staff_id}, 1, {$store_id})";
                        $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);
                    }

                    $alias = $boss_telephone . $store_id;

                    /*
                    $umeng = new Umeng();
                    $umeng->sendAndroidCustomizedcast('订单消息', $content, 'kkmOA', $alias);
                    $umeng->sendIOSCustomizedcast($content, 'kkmOA', $alias, '', '', false);
                    */
				} else {
					$pdo->rollBack();
				}

			}
		}
		
		
	} else if ($type == $type_config['type_3']) {
		//VIP卡

		// 修改订单状态
		// 修改用户金额
		// 写入vip卡资金流水
		// 修改对应vip卡销售数量和销售金额
		// 修改店铺余额
		// 写入店铺资金流水
        // 回调成功后写入消息

		//订单信息
		$order_query_sql = 'SELECT id, store_id, ctm_id FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
		$order_query_stat = $pdo->prepare($order_query_sql);
		$order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
		if ($order_query_result) {
			$order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
			if ($order_query_row) {
				$order_id = $order_query_row['id'];
				$store_id = $order_query_row['store_id'];
				$ctm_id = $order_query_row['ctm_id'];

				//订单产品信息
				$ord_pro_query_sql = 'SELECT id, pro_id, number FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
				$ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
				$ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
				$ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
				$pro_id = $ord_pro_query_row['pro_id'];
                $number = $ord_pro_query_row['number'];

				//用户信息
				$custom_query_sql = 'SELECT id, custom_phone, custom_name, charge_money, real_money, consume_money FROM da_custom WHERE id = :id';
				$custom_query_stat = $pdo->prepare($custom_query_sql);
				$custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
				$custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
				$custom_phone = $custom_query_row['custom_phone'];
                $custom_name = $custom_query_row['custom_name'];
				$charge_money = $custom_query_row['charge_money'];
				$real_money = $custom_query_row['real_money'];
				$consume_money = $custom_query_row['consume_money'];

				//店铺信息
				$store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
				$store_query_stat = $pdo->prepare($store_query_sql);
				$store_query_result = $store_query_stat->execute([':id' => $store_id]);
				$store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
				$store_money = $store_query_row['money'];

				//vip卡信息
				$vipcard_query_sql = 'SELECT id, vip_name, vip_lines, vip_discount FROM da_vipcard WHERE id = :id';
				$vipcard_query_stat = $pdo->prepare($vipcard_query_sql);
				$vipcard_query_result = $vipcard_query_stat->execute([':id' => $pro_id]);
				$vipcard_query_row = $vipcard_query_stat->fetch(\PDO::FETCH_ASSOC);
				$vipcard_id = $vipcard_query_row['id'];
                $vip_name = $vipcard_query_row['vip_name'];
				$vip_lines = $vipcard_query_row['vip_lines'];
				$vip_discount = $vipcard_query_row['vip_discount'];
				
				
				$pdo->beginTransaction();
				
				$time = time();
				$flow_number = $custom_phone . $time . mt_rand(1000, 9999);

				//修改订单状态
				$order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_4']}, status = {$status_config['STATUS_11']}, pay_time = {$time}, update_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
				$order_status_result = $pdo->exec($order_status_sql);

				//修改用户金额
				$vip_real_money = floor($vip_lines / ($vip_discount / 10) * 100) / 100;
				$custom_money_sql = "UPDATE da_custom SET vip_status = 2, charge_money = charge_money + {$vip_lines}, real_money = real_money + {$vip_real_money}, consume_money = consume_money + {$vip_lines} WHERE id = {$ctm_id}";
				$custom_money_result = $pdo->exec($custom_money_sql);

				//写入vip卡资金流水
				$last_money = $real_money + $vip_real_money;
				$ctmflow_insert_sql = "INSERT INTO da_ctmmoneyflow (type, pay_method, store_id, ctm_id, item_id, money, last_money, flow_number, add_time) VALUES (1, {$ordtype_config['ordtype_4']}, {$store_id}, {$ctm_id}, {$pro_id}, {$vip_real_money}, {$last_money}, '{$flow_number}', {$time})";
				$ctmflow_insert_result = $pdo->exec($ctmflow_insert_sql);

				//修改对应vip卡销售数量和销售金额
				$vipcard_update_sql = "UPDATE da_vipcard SET sell_count = sell_count + 1, sell_money = sell_money + {$vip_lines} WHERE id = {$pro_id}";
				$vipcard_update_result = $pdo->exec($vipcard_update_sql);

				//修改店铺余额
				$store_money_sql = "UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}";
				$store_money_result = $pdo->exec($store_money_sql);

				//写入店铺资金流水
				$flow_last_money = $store_money + $total_amount;
				$flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_3']}, {$moneyflow_config['pay_method_4']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
				$flow_insert_result = $pdo->exec($flow_insert_sql);

				if ($order_status_result && $custom_money_result && $ctmflow_insert_result && $vipcard_update_result && $store_money_result && $flow_insert_result) {
					$pdo->commit();

                    //回调成功后写入消息

                    //老板信息
                    $staff_query_sql = 'SELECT ds.id, ds.telephone FROM da_staff_store dss INNER JOIN da_staff ds ON dss.staff_id = ds.id WHERE dss.store_id = :store_id';
                    $staff_query_stat = $pdo->prepare($staff_query_sql);
                    $staff_query_result = $staff_query_stat->execute([':store_id' => $store_id]);
                    $staff_query_row = $staff_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $boss_id = $staff_query_row['id'];
                    $boss_telephone = $staff_query_row['telephone'];

                    $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $content = "【已付款】[{$custom_name}] [{$vip_name}] X [{$number}]，共计 [{$total_amount}] 元";
                    $msgContent = getColoredString($content);
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('1', {$order_id}, 0, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    //收入消息
                    $content = "【新增收入】最新收入 [{$total_amount}] 元，请注意查收";
                    $msgContent = getColoredString($content);
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('10', {$order_id}, 0, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    $alias = $boss_telephone . $store_id;

                    /*
                    $umeng = new Umeng();
                    $umeng->sendAndroidCustomizedcast('订单消息', $content, 'kkmOA', $alias);
                    $umeng->sendIOSCustomizedcast($content, 'kkmOA', $alias, '', '', false);
                    */
				} else {
					$pdo->rollBack();
				}
			}
		}
		

	} else if ($type == $type_config['type_4']) {
		//体验卡

		// 修改订单状态
		// 修改用户金额
		// 写入体验卡购买记录
		// 修改店铺余额
		// 写入店铺资金流水
        // 回调成功后写入消息

		//订单信息
		$order_query_sql = 'SELECT id, store_id, ctm_id FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
		$order_query_stat = $pdo->prepare($order_query_sql);
		$order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
		if ($order_query_result) {
			$order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
			if ($order_query_row) {
				$order_id = $order_query_row['id'];
				$store_id = $order_query_row['store_id'];
				$ctm_id = $order_query_row['ctm_id'];

				//订单产品信息
				$ord_pro_query_sql = 'SELECT id, pro_id, number FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
				$ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
				$ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
				$ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
				$pro_id = $ord_pro_query_row['pro_id'];
                $number = $ord_pro_query_row['number'];

				//用户信息
				$custom_query_sql = 'SELECT id, custom_phone, custom_name, consume_money FROM da_custom WHERE id = :id';
				$custom_query_stat = $pdo->prepare($custom_query_sql);
				$custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
				$custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
				$custom_phone = $custom_query_row['custom_phone'];
                $custom_name = $custom_query_row['custom_name'];
				$consume_money = $custom_query_row['consume_money'];

				//店铺信息
				$store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
				$store_query_stat = $pdo->prepare($store_query_sql);
				$store_query_result = $store_query_stat->execute([':id' => $store_id]);
				$store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
				$store_money = $store_query_row['money'];


				$pdo->beginTransaction();
				
				$time = time();
				$flow_number = $custom_phone . $time . mt_rand(1000, 9999);

				//修改订单状态
				$order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_4']}, status = {$status_config['status_2']}, pay_time = {$time}, update_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
				$order_status_result = $pdo->exec($order_status_sql);

				//修改用户金额
				$custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
				$custom_money_result = $pdo->exec($custom_money_sql);

				//写入体验卡购买记录
				$start_time = time();
				$end_time = $start_time + 31 * 24 * 3600;
				$expercard_custom_insert_sql = "INSERT INTO da_expercard_custom (store_id, expercard_id, custom_id, price, start_time, end_time, add_time) VALUES ({$store_id}, {$pro_id}, {$ctm_id}, {$total_amount}, {$start_time}, {$end_time}, {$start_time})";
				$expercard_custom_insert_result = $pdo->exec($expercard_custom_insert_sql);

				//修改店铺余额
				$store_money_sql = "UPDATE da_store SET money = money + {$total_amount}  WHERE id = {$store_id}";
				$store_money_result = $pdo->exec($store_money_sql);

				//写入店铺资金流水
				$flow_last_money = $store_money + $total_amount;
				$flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_4']}, {$moneyflow_config['pay_method_4']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
				$flow_insert_result = $pdo->exec($flow_insert_sql);

				if ($order_status_result && $custom_money_result && $expercard_custom_insert_result && $store_money_result && $flow_insert_result) {
					$pdo->commit();

                    //回调成功后写入消息

                    //产品信息
                    $expercard_query_sql = 'SELECT id, name FROM da_expercard WHERE id = :id';
                    $expercard_query_stat = $pdo->prepare($expercard_query_sql);
                    $expercard_query_result = $expercard_query_stat->execute([':id' => $pro_id]);
                    $expercard_query_row = $expercard_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $expercard_name = $expercard_query_row['name'];

                    //老板信息
                    $staff_query_sql = 'SELECT ds.id, ds.telephone FROM da_staff_store dss INNER JOIN da_staff ds ON dss.staff_id = ds.id WHERE dss.store_id = :store_id';
                    $staff_query_stat = $pdo->prepare($staff_query_sql);
                    $staff_query_result = $staff_query_stat->execute([':store_id' => $store_id]);
                    $staff_query_row = $staff_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $boss_id = $staff_query_row['id'];
                    $boss_telephone = $staff_query_row['telephone'];

                    $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $content = "【已付款】[{$custom_name}] [{$expercard_name}] X [{$number}]，共计 [{$total_amount}] 元";
                    $msgContent = getColoredString($content);
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('1', {$order_id}, 0, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    //收入消息
                    $content = "【新增收入】最新收入 [{$total_amount}] 元，请注意查收";
                    $msgContent = getColoredString($content);
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('10', {$order_id}, 0, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    $alias = $boss_telephone . $store_id;

                    /*
                    $umeng = new Umeng();
                    $umeng->sendAndroidCustomizedcast('订单消息', $content, 'kkmOA', $alias);
                    $umeng->sendIOSCustomizedcast($content, 'kkmOA', $alias, '', '', false);
                    */
				} else {
					$pdo->rollBack();
				}

			}
		}
		

	} else if ($type == $type_config['type_5']) {
		//限时抢购

		// 修改订单状态
		// 修改用户金额
		// 限时抢购减少库存
		// 写入限时抢购购买记录
		// 修改店铺余额
		// 写入店铺资金流水
		// 写入预约记录如果购买的时候选择了预约时间
        // 回调成功后写入消息

		//订单信息
		$order_query_sql = 'SELECT id, store_id, ctm_id, staff_id, starttime FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
		$order_query_stat = $pdo->prepare($order_query_sql);
		$order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
		if ($order_query_result) {
			$order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
			if ($order_query_row) {
				$order_id = $order_query_row['id'];
				$store_id = $order_query_row['store_id'];
				$ctm_id = $order_query_row['ctm_id'];
				$staff_id = $order_query_row['staff_id'];
				$starttime = $order_query_row['starttime'];

				//订单产品信息
				$ord_pro_query_sql = 'SELECT id, pro_id, number FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
				$ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
				$ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
				$ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
				$pro_id = $ord_pro_query_row['pro_id'];
				$number = $ord_pro_query_row['number'];
				
				//限时抢购信息
				$miaosha_query_sql = 'SELECT id, item_id FROM da_miaosha WHERE id = :id';
				$miaosha_query_stat = $pdo->prepare($miaosha_query_sql);
				$miaosha_query_result = $miaosha_query_stat->execute([':id' => $pro_id]);
				$miaosha_query_row = $miaosha_query_stat->fetch(\PDO::FETCH_ASSOC);
				$item_id = $miaosha_query_row['item_id'];
				
				//用户信息
				$custom_query_sql = 'SELECT id, custom_phone, custom_name, consume_money FROM da_custom WHERE id = :id';
				$custom_query_stat = $pdo->prepare($custom_query_sql);
				$custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
				$custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
				$custom_phone = $custom_query_row['custom_phone'];
				$custom_name = $custom_query_row['custom_name'];
				$consume_money = $custom_query_row['consume_money'];

				//店铺信息
				$store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
				$store_query_stat = $pdo->prepare($store_query_sql);
				$store_query_result = $store_query_stat->execute([':id' => $store_id]);
				$store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
				$store_money = $store_query_row['money'];


				$pdo->beginTransaction();
				
				$time = time();
				$flow_number = $custom_phone . $time . mt_rand(1000, 9999);

				//写入预约记录
                $last_yuyue_id = 0;
				if ($staff_id == 0) {
					$ctm_staff_insert_result = true;
				} else {
					$ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
					$ctm_staff_insert_sql = "INSERT INTO da_ctm_staff (staff_id, ctm_id, item_id, store_id, order_id, number, ctm_name, ctm_phone, status, add_time, start_time) VALUES ({$staff_id}, {$ctm_id}, {$item_id}, {$store_id}, {$order_id}, 1, '{$ctm_name}', '{$custom_phone}', 3, {$time}, {$starttime})";
					$ctm_staff_insert_result = $pdo->exec($ctm_staff_insert_sql);
                    $last_yuyue_id = $pdo->lastInsertId();
				}
				
				//修改订单状态
				$status = $status_config['status_2'];
				if ($staff_id != 0) {
					if ($number == 1) {
						$status = 3;
					}
				}
				$order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_4']}, status = {$status}, pay_time = {$time}, update_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
				$order_status_result = $pdo->exec($order_status_sql);

				//修改用户金额
				$custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
				$custom_money_result = $pdo->exec($custom_money_sql);

				//限时抢购减少库存
				$miaosha_update_sql = "UPDATE da_miaosha SET number = number - 1, sell_count = sell_count + 1 WHERE id = {$pro_id}";
				$miaosha_update_result = $pdo->exec($miaosha_update_sql);

				//写入限时抢购购买记录
				$miaosha_custom_insert_sql = "INSERT INTO da_miaosha_custom (store_id, miaosha_id, custom_id, number, price, add_time) VALUES ({$store_id}, {$pro_id}, {$ctm_id}, {$number}, {$total_amount}, {$time})";
				$miaosha_custom_insert_result = $pdo->exec($miaosha_custom_insert_sql);

				//修改店铺余额
				$store_money_sql = "UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}";
				$store_money_result = $pdo->exec($store_money_sql);

				//写入店铺资金流水
				$flow_last_money = $store_money + $total_amount;
				$flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_5']}, {$moneyflow_config['pay_method_4']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
				$flow_insert_result = $pdo->exec($flow_insert_sql);
				
				if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_update_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result) {
					$pdo->commit();

                    //回调成功后写入消息

                    //产品信息
                    $item_query_sql = 'SELECT id, item_name FROM da_item WHERE id = :id';
                    $item_query_stat = $pdo->prepare($item_query_sql);
                    $item_query_result = $item_query_stat->execute([':id' => $item_id]);
                    $item_query_row = $item_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $item_name = $item_query_row['item_name'];

                    //老板信息
                    $staff_query_sql = 'SELECT ds.id, ds.telephone FROM da_staff_store dss INNER JOIN da_staff ds ON dss.staff_id = ds.id WHERE dss.store_id = :store_id';
                    $staff_query_stat = $pdo->prepare($staff_query_sql);
                    $staff_query_result = $staff_query_stat->execute([':store_id' => $store_id]);
                    $staff_query_row = $staff_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $boss_id = $staff_query_row['id'];
                    $boss_telephone = $staff_query_row['telephone'];

                    $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $content = "【已付款】[{$custom_name}] [{$item_name}] X [{$number}]，共计 [{$total_amount}] 元";
                    $msgContent = getColoredString($content);
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('1', {$order_id}, 0, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    //收入消息
                    $content = "【新增收入】最新收入 [{$total_amount}] 元，请注意查收";
                    $msgContent = getColoredString($content);
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('10', {$order_id}, 0, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    //预约消息
                    if ($staff_id != 0) {
                        //给店铺发推送

                        //美容师信息
                        $staff_query_sql_2 = 'SELECT id, staff_name FROM da_staff WHERE id = :id';
                        $staff_query_stat_2 = $pdo->prepare($staff_query_sql_2);
                        $staff_query_result_2 = $staff_query_stat_2->execute([':id' => $staff_id]);
                        $staff_query_row_2 = $staff_query_stat_2->fetch(\PDO::FETCH_ASSOC);
                        $staff_name = $staff_query_row_2['staff_name'];

                        $yuyue_time = date('Y-m-d H:i', $starttime);
                        $content = "【已预约】[{$custom_name}] 预约了 [{$yuyue_time}] [{$staff_name}] 的 [{$item_name}]，快去确认吧";
                        $msgContent = getColoredString($content);
                        $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('2', {$order_id}, $last_yuyue_id, \"{$msgContent}\", '{$time}', {$boss_id}, 1, {$store_id})";
                        $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                        //给美容师发推送
                        $content = "【已预约】[{$custom_name}] [{$yuyue_time}] 向您预约了 [{$item_name}] 服务";
                        $msgContent = getColoredString($content);
                        $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, yuyue_id, content, add_time, userid, type, store_id) VALUES ('2', {$order_id}, $last_yuyue_id, \"{$msgContent}\", '{$time}', {$staff_id}, 1, {$store_id})";
                        $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);
                    }

                    $alias = $boss_telephone . $store_id;

                    /*
                    $umeng = new Umeng();
                    $umeng->sendAndroidCustomizedcast('订单消息', $content, 'kkmOA', $alias);
                    $umeng->sendIOSCustomizedcast($content, 'kkmOA', $alias, '', '', false);
                    */
				} else {
					$pdo->rollBack();
				}

			}
		}

	}
	
	
	echo 'success';
} else {
	echo 'fail';
}


?>